package com.linzm.leetcode.primary.exercises4_20230129;

import java.util.Arrays;

/**
 * @Author zimingl
 * @Date 2023/1/29 22:25
 * @Description: 重新排列数组
 */
public class Demo1_1470 {
    /**
     * 输入：nums = [2,5,1,3,4,7], n = 3
     * 输出：[2,3,5,4,1,7]
     * 解释：由于 x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 ，所以答案为 [2,3,5,4,1,7]
     */
    public static void main(String[] args) {
        int[] nums = {2, 5, 1, 3, 4, 7};
        int n = 3;
        Demo1_1470 demo1_1470 = new Demo1_1470();
        int[] shuffle = demo1_1470.shuffle(nums, n);
        String[] strings = Arrays.stream(shuffle).mapToObj(String::valueOf).toArray(String[]::new);
        System.out.println(String.join(" ", strings));
    }

    private int[] shuffle(int[] nums, int n) {
        int[] newArr = new int[nums.length];
        for (int i = 0, j = 0; j < n; ) {
            newArr[i] = nums[j];
            newArr[i + 1] = nums[n + j];
            i += 2;
            j++;
        }
        return newArr;
    }
}
